<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Administrator_advertisement extends MY_Administrator {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct();		
		//Load Model
		$this->load->model('administrator_advertisement_model');
    }

	/*Content Management*/
	public function content_management($module=NULL,$arg=NULL)
	{
		switch($module):
			case 'add':
				//Load Datetime
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
				$this->data['js'][] = "jquery-ui-sliderAccess.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";			
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_advertisement_model->content_management_add();
					redirect($this->session->userdata('returnUrl'));
				endif;
				
				$this->data['content'] = "administrator/advertisement/content_management_add";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
			break;
			case 'edit':
				$id = intval($arg);
				//Check Id exist
				$is_exist = $this->db->from('ads')
				->where(array('ads_id' => $id))
				->count_all_results();
				if(empty($is_exist)) show_error('This id is not information.');	

				//Load Datetime
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
				$this->data['js'][] = "jquery-ui-sliderAccess.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";								
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_advertisement_model->content_management_edit($id);
					redirect($this->session->userdata('returnUrl'));			
				endif;							

				$this->data['dataResult'] = $this->db->get_where('ads as t',array('t.ads_id' => $id),1)->row();
	
				$this->data['content'] = "administrator/advertisement/content_management_edit";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
			break;			
			case 'delete':
				$this->administrator_advertisement_model->content_management_delete();	
				redirect($this->session->userdata('returnUrl'));		
			break;												
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
					
				/*Order Condition*/
				$orderby = $this->input->get('orderby');
				$sort = $this->input->get('sort');
				$orderCondition = "ads_id DESC";
				if(!empty($orderby) && !empty($sort)):
					$orderCondition = $orderby." ".$sort;
				endif;
				
				/*Search Condition*/
				$searchCondition = array('ads_id IS NOT NULL');
				$ads_id = $this->input->get('ads_id');
				$ads_name = addslashes($this->input->get('ads_name'));
				$update_by = addslashes($this->input->get('update_by'));
				if(!empty($ads_id)) $searchCondition[] = "ads_id = ".$ads_id."";
				if(!empty($ads_name)) $searchCondition[] = "t.ads_name LIKE '%".$ads_name."%'";
				if(!empty($update_by)) $searchCondition[] = "t.update_by = '".$update_by."'";
				
			  	/*Pager*/		
				$config['base_url'] = current_url().'?'.query_string(array('per_page'));
				$config['total_rows'] = $this->db->from('ads')->where(''.implode(' AND ',$searchCondition).'')->count_all_results();
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
			  	$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->order_by($orderCondition)->get_where('ads',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();
				
				$this->data['content'] = "administrator/advertisement/content_management";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
		endswitch;		
	}

	public function upload_mp4($module=NULL,$arg=NULL)
	{
		switch($module):
			case 'delete':
				$this->administrator_advertisement_model->upload_mp4_delete();	
				redirect($this->session->userdata('returnUrl'));
			break;
			default:
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_advertisement_model->upload_mp4();
					redirect($this->session->userdata('returnUrl'));			
				endif;
				
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));				
				
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";
								
				$this->data['dataResult'] = directory_map($this->upload_folder.'../../ads/', 1);
				
				$this->data['content'] = "administrator/advertisement/upload_mp4";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);				
		endswitch;	
	}
	
	public function statics($module=NULL,$arg=NULL)
	{
		//Load Calendar
		$this->data['js'][] = "jquery.calendar.js";
		$this->data['css'][] = "calendar.css";
		
		//Load Jqplot
		$this->data['js'][] = "jqplot/jquery.jqplot.min.js";
		$this->data['js'][] = "jqplot/jqplot.highlighter.min.js";
		$this->data['js'][] = "jqplot/jqplot.cursor.min.js";
		$this->data['js'][] = "jqplot/jqplot.dateAxisRenderer.min.js";		
		
		$start_date = $this->input->get('start_date');
		$end_date = $this->input->get('end_date');
		$searchCondition = "t.date = '".date('Y-m-d')."'";
		if($start_date && $end_date):
			$start_date_arr = explode('/',$start_date);
			$end_date_arr = explode('/',$end_date);
			$searchCondition = "t.date >= '".$start_date_arr[2]."-".$start_date_arr[1]."-".$start_date_arr[0]."' AND t.date <= '".$end_date_arr[2]."-".$end_date_arr[1]."-".$end_date_arr[0]." 23:59:59'";
		endif;
		$ads_id = $this->input->get('ads_id');
		if(!empty($ads_id)) $searchCondition .= " AND t.ads_id = ".intval($ads_id)."";
		
		$this->data['dataResult'] = $this->db->select("t.ads_id,j.ads_name,SUM(CASE WHEN t.event='Impression' THEN t.views ELSE 0 END) as Impression,SUM(CASE WHEN t.event='click' THEN t.views ELSE 0 END) as Click,t.date")->
		join('ads as j',"j.ads_id=t.ads_id",'INNER')->
		order_by('t.date DESC,j.ads_name ASC')->
		group_by('t.ads_id,t.date')->
		get_where('ads_static as t',$searchCondition)->
		result();
		
		//Create jqPlot Data
		$this->data['dataJqplot'] = array();
		$dataJqplot = array();
		foreach($this->data['dataResult'] as $value):
			$dataJqplot[$value->ads_id][] = array(
				$value->date,
				intval($value->Impression)
			);
		endforeach;
		foreach($dataJqplot as $key => $value):
			$this->data['dataJqplot'][] = $value;
		endforeach;
						
		$this->data['content'] = "administrator/advertisement/statics";
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
	}		
}